<?php
/**
 *@author Connor <caokang@foxmail.com>
 *@todo
 *@link www.zeevin.com
 */
class SettingController extends BaseController{
	private $_model;

	public function actionIndex(){

		$model = $this->loadRecord();

		if(Yii::app()->request->isAjaxRequest){
			// $model->scenario = 'modify';
			if(isset($_POST['User']['space_privacy']))$model->space_privacy = $_POST['User']['space_privacy'];
			if(isset($_POST['User']['alert_setting']))$model->alert_setting =$_POST['User']['alert_setting'];
			$model->password_tmp='hashhash';
			if(isset($_POST['User']['status']))$model->status=$_POST['User']['status'];
			if($model->save()){
				$ret='0';
			}else{
				$ret='1';
				print_r($model->errors);
			}
			exit($ret);
		}

		$this->render('index',array('model'=>$model));
	}


	public function actionEmail(){
		$model = $this->loadRecord();
		if(Yii::app()->request->isAjaxRequest){
			$model->scenario = 'modify'; 
			if($model->email != $_POST['User']['old_email']){exit('1');}//原始邮箱输入不正确
 			if($model->password != crypt($_POST['User']['repassword'],$model->password)){exit('2');}//密码不正确

			$model->email = $_POST['User']['new_email'];
			if($model->validate() && $model->save()){
					$ret='0';
				}else{
					$ret='3';
				}
				
			
			exit($ret);
		}
		$this->render('email',array('model'=>$model));
	}

	public function actionPassword(){
		$model = $this->loadRecord();
		if(Yii::app()->request->isAjaxRequest){
			$model->scenario = 'modify'; 
			if($model->password != crypt($_POST['old_pass'],$model->password)){exit('2');}
			if($_POST['new_pass']!=$_POST['re_pass']){exit('1');}
			$pass = crypt(trim($_POST['new_pass']));
			$model->password = crypt(trim($_POST['new_pass']),$pass);

			if($model->validate() && $model->save()){
				$ret='0';
			}else{
					$ret='3';
				}

			exit($ret);
		}
		$this->render('password');
	}

	public function actionBase(){
		$User = $this->loadRecord();
		$Setting = UserSetting::model()->find("uid=:uid",array('uid'=>$this->uid));
		
		if(Yii::app()->request->isPostRequest){
						// print_r($_POST);exit;

			$User->scenario = 'modify';
			$Setting->scenario = 'modify';
			$User->attributes = $_POST['User'];
			$User->password_tmp = 'hackhack';
			$Setting->attributes = $_POST['UserSetting'];
			$Setting->sex_tendency = $_POST['sex_tendency'];
			if($Setting->validate()&&$User->validate()){
				$transcation = Yii::app()->db->beginTransaction();
				try {
					if(!$User->save()){
						$msg = "用户信息修改失败";
					}
					$Setting->save();
					$msg='基本信息修改成功';
					$transcation->commit();
				} catch (Exception $e) {
					$transcation->rollback();
				}
			}
			// print_r($Setting->errors);

		}
		$this->render('base',array('user'=>$User,'setting'=>$Setting,'msg'=>$msg));
	}

	public function actionAvatar(){
		$model = User::model()->findByPk($this->uid);
		// var_dump($model);
		if(Yii::app()->request->isAjaxRequest && $model->avatar){
			unset($_GET['r']);
			if(!empty($_GET)){
				if($model->avatar=='avatar.jpg'){
					$msg['status']=1;
					$msg['desc']='请先上传一张自已的图片再剪切';
				}else{
					$width = $_GET['width'];
					$height = $_GET['height'];
					$thumb = Utils::genThumb(Utils::getAvatar($model->avatar),$_GET['x1'],$_GET['y1'],$width,$height);
					if($thumb){
						$msg['status']=0;
						$msg['desc']='头像缩略图保存成功';
					}else{
						$msg['status']=1;
						$msg['desc']='头像缩略图保存失败';
					}
				}
				
				print_r(json_encode($msg));exit;
			}
			// print_r($_GET);exit;

		}

		if($_POST){
			$avatar = CUploadedFile::getInstance($model,'avatar');

			$hash_name = $this->uid.'-'.substr(time(),6).rand(100,999).'.'.$avatar->extensionName;

			$model->avatar = $hash_name;
			$model->password_tmp = 'hashhash';
			// echo $hash_name;
			if(!empty($avatar) && $model->save()){
				// var_dump($avatar);
				file_exists($this->avatar_dir)?null:mkdir($this->avatar_dir);
				if($avatar->saveAs($this->avatar_dir.$hash_name)){
					$msg['status']=0;
					$msg['desc']='图片上传成功';
					Utils::resizeAvatar($this->avatar_dir.$hash_name);
					$this->refresh();
				}else{
					$msg['status']=1;
					$msg['desc']='图片上传失败：'.$avatar->error;

				}
			}else{
				// print_r($model->errors);
				$msg['status']=1;
			}
		}
		$this->render('avatar',array('model'=>$model));
	}












	public function loadRecord(){
		if($this->_model===null)
		{
			$this->_model=User::model()->findByPk($this->uid);
		}
		return $this->_model;
	}
}